//app.js

var util = require('utils/util')


var that;

App({
  screenWidth:0,
  screenHeight:0,
  onLaunch: function () {
      
    //调用API从本地缓存中获取数据
    that = this;
    var user = wx.getStorageSync('user') || {};
    var userInfo = wx.getStorageSync('userInfo') || {};
    if (!user.openid) {
      that.userLogin();
    }
    wx.getSystemInfo({
      success: function(res) {
        that.screenWidth = res.windowWidth;
        that.screenHeight = res.windowHeight;
      },
    });
    wx.setStorageSync('questionInfo', []);
  },
  getUserInfo:function(cb){
   
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              const userInfo = {...res.userInfo};
              
              typeof cb == "function" && cb(userInfo);
            }
          })
        }
      })
    }
  },
  userLogin: function (cb) {

    //用户登录
    wx.login({
      success: function (res) {
        if (res.code) {

          //用于使用code换取openid
          util.sendRequest('WxGetOpenIdByCode', {
            code: res.code
          }, function (resp) {


            if (resp && +resp.code === 0) {
              that.globalData.openId = resp.text
              
              wx.setStorageSync('openid', resp.text);
            } else {
              util.wxModalSimpleAlert('用户登录失败！')
            }

            that.getUserInfo(function(user){
   
              util.sendRequest('WxLogin', user, function (resp) {

                if (resp && resp.code == 0) {
                  var userInfo = resp.result;

                  wx.setStorageSync("userInfo", userInfo);
                  wx.setStorageSync("isLoginCompelete", true);
                  // 已完成问卷调查
                  // if (userInfo.isOverQuestion){
                  //   wx.redirectTo({
                  //     url: '/pages/index/index',
                  //   })
                  // }else{
                  //   wx.redirectTo({
                  //     url: '/pages/choose/version',
                  //   })
                  // }
                }else{
                  util.wxModalSimpleAlert('用户登录失败！');
                }
              }, '登录中...',error=>{



              });
            });
            
            typeof cb == "function" && cb(that.globalData.openId)
          },
            '正在登录...',
            function (error) {
              util.wxModalSimpleAlert('用户登录失败！')
            })
        } else {
          util.wxModalSimpleAlert('用户登录失败！')
        }
      },
      fail: function (res) {
        // fail
        util.wxModalSimpleAlert('用户登录失败！')
      },
      complete: function () {
        // complete
      }
    })
  },
  globalData:{
    userInfo:null,
    user:null,
    appid:'wxb69ba32600af80f4',
    secret:'c3ff00133c6611dd07618a7b7ff6e58b',
    openId:''
  }
})